<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UploadVideoController extends BaseController
{
    public function doUploadFile(Request $request)
    {
        //存在文件且合法
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            //获取传递过来的文件
            $file = $request->file('file');
            //设置合法后缀名
            $allowed_extensions = ["png", "jpg", "jpeg", "gif", "doc", "docx", "txt", "pdf", "xls", "xlsx", "ppt", "pptx", "mp3", "mp4","wav"];
            //如果上传的文件后缀名不在定义数组
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                return $this->ResultJson(0, '文件格式不正确!', []);
            } else {
                $destinationPath = 'upload/images/' . date("Y-m-d") . '/'; //public 文件夹下面建 uploads 文件夹
                //获取文件的后缀名
                $extension = $file->getClientOriginalExtension();
                //给上传文件定义文件名
                $fileName = md5(time() . rand(000000, 999999)) . '.' . $extension;
                //文件转换
                $file->move($destinationPath, $fileName);
                //设置文件路径
//                $filePath = asset($destinationPath . $fileName);
                $filePath = 'images/' . date("Y-m-d") . '/' . $fileName;
                return $this->ResultJson(1, '上传成功!', [$filePath]);
            }
        } else {
            return $this->ResultJson(0, '文件上传失败!', []);
        }
    }
}
